#!perl -w

#anal( "exif-exif.txt" );
#anal( "exif-gps.txt" );
anal( "exif-interop.txt" );

sub anal
{
    $f = shift();
    if ( "exif-tiff.txt" eq $f ) { return; }

    %key = ();
    %ind = ();

    @z = split( /\./, $f );
    $y = shift( @z );
    $y =~ tr/-/_/;

    open( XX, "<$f" );
    while($a = <XX>)
    {
        chomp( $a );
        if ( $a =~ /^[A-Z]\./ ) { next; }
        if ( $a =~ /^\s+$/ )    { next; }

        @b = split( /\s+/, $a );

        # find where is number.
        for( $c = 0; $c < $#b; ++ $c )
        {
            if ( $b[$c] =~ /^\d+$/ )
            {
                $d = int( $b[$c] );
                if ( $b[$c+1] eq sprintf( "%X", $d ) )
                {
                    last;
                }
            }
        }

        # 없는건 말이 안 되지.
        if ( $c > $#b )
        {
            printf( "$a\n" );
            next;
        }

        $tagid      = int($b[$c]);
        $tagidhex   = sprintf( "%08x", $tagid );

        $tagname    = $b[$c-1]; 
        $tagdesc    = join( " ", @b[ 0 .. $c-2] );
        $tagtype    = join( " ", @b[ $c+2 .. $#b ] );

        $tagdesc   =~ tr/ /_/;
        $tagtype   =~ tr/ /_/;

        $str = sprintf( "%d %s %s %s", $tagid, $tagname, $tagdesc, $tagtype );
        $key{$tagidhex} = $str;
        $ind{$tagidhex} = $tagid;
    }

    @keys = sort keys %key;

    foreach $k ( @keys )
    {
        $v = $key{$k};
        printf( "%s\n", $v );
    }
}


